/**
 * @author chongw
 * @since 2021-06-01 14:24:29
 */
layui.define(['form', 'layedit', 'laydate','zmnSelect','zmnApp', 'zmnBind'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let zmnBind = layui.zmnBind;
    let zmnApp = layui.zmnApp;
    let zmnSelect = layui.zmnSelect;

    form.render(null, "zmn-form");

    // 自定义验证规则
    form.verify({
        sort: function(value){
            if (!/^[0-9]+$/.test(value)) {
                return "排序值只能是正整数";
            }
        },
        integer: [
            /^[0-9]*([.][0-9]{0,2})?$/
            , '只能输入正整数或小数点后保留两位'
        ]
    });

    zmnSelect.selectlAreas({
        provinceSelectId: 'provinceId',
        citySelectId: 'cityId'
    }, {
        provinceSelectId: {
            selectedValues: json.provinceId
        },
        citySelectId: {
            selectedValues: json.cityId
        }
    }, {
        isRadio: true
    });

    zmnSelect.selectlServBizUnits({
        bizTypeSelectId: 'servClusterType',
        bizLineSelectId: 'servClusterId',
        prodGroupSelectId: 'servGroupId'
    },  {
        bizTypeSelectId: {
            selectedValues: json.servClusterType
        },
        bizLineSelectId: {
            selectedValues: json.servClusterId
        },
        prodGroupSelectId: {
            selectedValues: json.servGroupId
        }
    }, {
        isRadio: true
    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let index;
        let data = obj.field;
        data.provinceName = $("#provinceId .label-content").attr("title");
        data.cityName = $("#cityId .label-content").attr("title");
        data.servClusterTypeName = $("#servClusterType .label-content").attr("title");
        data.servClusterName = $("#servClusterId .label-content").attr("title");
        data.servGroupName = $("#servGroupId .label-content").attr("title");
        $.ajax({
            async: false,
            type: "POST",
            url: "/brs/oreder/rule/comparison/addEdit.action",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success:function(res) {
                if (res.status === 200) {
                    layer.msg((data.thresholdId?"修改":"新增")+"配置规则成功！",{"icon": 1,"time":1000},function (){
                        parent.location.reload();
                    });
                } else {
                    layer.msg(res.message, {"icon": 2});
                }
                layer.close(index);
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){}
        })
        return false;
    });

    //对外暴露的接口
    exports('brs_order_rule_comparison_detail', {});
});

// 取消
function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}